<template>
  <!-- 检验类别弹窗 -->
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item  label="检验类别编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input readOnly v-decorator="[ 'testcode', validatorRules.testcode]" placeholder="请输入检验类别编号"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="检验类别名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'testname', validatorRules.testname]" placeholder="请输入检验类别名称"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--<j-dict-select-tag  type="radio" v-decorator="['teststate', validatorRules.teststate]" :trigger-change="true" dictCode="mes_status" placeholder="请选择状态"/>-->

              <p-radio-group name="radioGroup" v-decorator="['teststate', validatorRules.teststate]">
                <p-radio value="1">
                  生效
                </p-radio>
                <p-radio value="2">
                  失效
                </p-radio>
              </p-radio-group>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, putAction, api } from "@/api/manage";
import fSwitch from "@/components/switch";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";
import { validateDuplicateValue } from "@/utils/util";

export default {
  name: "MesQcTestTypeModal",
  mixins: [SubmitOnchangMixin],
  components: {
    fSwitch,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        testcode: {
          rules: [{ required: true, message: "请输入检验类别编号!" }],
        },
        testname: {
          rules: [{ required: true, message: "请输入检验类别名称!" }],
        },
        teststate: { rules: [{ required: true, message: "请输入状态!" }] },
        remark: {},
      },
      url: {
        add: "/mes-quality/mesQcTestType/add",
        edit: "/mes-quality/mesQcTestType/edit",
      },
    };
  },
  created() {},
  mounted() {
    this.form.setFieldsValue({ teststate: '1'});
    this.model.teststate = '1';
  },
  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
    },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [{ ruleCode: "mes_testcode", columnName: "testcode" }],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" + res.data);
          const item = res.data;
          item["teststate"] = '1'; //表单默认值赋值区域
          this.form.setFieldsValue(pick(item, Object.keys(item)));
        }
      });
    },

    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "testcode",
            "testname",
            "teststate",
            "createBy",
            "createTime",
            "updateBy",
            "updateTime",
            "remark"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "testcode",
          "testname",
          "teststate",
          "createBy",
          "createTime",
          "updateBy",
          "updateTime",
          "remark"
        )
      );
    },
  },
};
</script>
